//
//  MBProgressHUD+Extension.swift
//  AIPhotoProcess
//
//  Created by aw031 on 17/10/2025.
//

import Foundation
import UIKit

private let afterDelay = 1.5

extension MBProgressHUD {
    /// 显示纯文本Toast
    @discardableResult
    class func showToast(message: String?, in superView: UIView? = nil) -> MBProgressHUD? {
        return MBProgressHUD.showToast(message: message, icon: nil, in: superView)
    }
    /// 显示加载中指示器
    @discardableResult
    class func showActivityToast(message: String? = "", in superView: UIView? = nil) -> MBProgressHUD? {
        let view = superView ?? KWindow!
  
        let hud = MBProgressHUD.show(for: view, animated: true)
        hud.label?.text = message
        hud.label?.numberOfLines = 0
        hud.label?.textColor = ColorWhite
        hud.label?.font = FontScale(14)
        hud.contentColor = ColorWhite
        hud.bezelView?.style = .solidColor
        hud.bezelView?.backgroundColor = ColorBlack
        hud.mode = .indeterminate
        hud.removeFromSuperViewOnHide = true
        return hud
    }
    /// 显示加载中指示器，指定时间后消失
    @discardableResult
    class func showActivityToast(message: String?, in superView: UIView? = nil, afterDelay time: TimeInterval) -> MBProgressHUD? {
        let view = superView ?? KWindow!
        let hud = MBProgressHUD.show(for: view, animated: true)
        hud.mode = .indeterminate
        hud.label?.textColor = ColorWhite
        hud.label?.font = FontScale(14)
        hud.label?.numberOfLines = 0
        hud.bezelView?.style = .solidColor
        hud.contentColor = ColorWhite
        hud.bezelView?.backgroundColor = ColorBlack
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: time)
        return hud
    }
    /// 显示带图片或不带图片的Toast
    @discardableResult
    class func showToast(message: String?, icon: String?, in superView: UIView? = nil) -> MBProgressHUD? {
        let view = superView ?? KWindow!

        let hud = MBProgressHUD.show(for: view, animated: true)
        hud.label?.text = message
        if let icon = icon {
            // 优先查找bundle图片
            let bundleImage = UIImage(named: "MBProgressHUD.bundle/\(icon)")
            let img = bundleImage ?? UIImage(named: icon)
            hud.customView = UIImageView(image: img)
            hud.mode = .customView
        } else {
            hud.mode = .text
        }
        hud.label?.textColor = ColorWhite
        hud.label?.font = FontScale(14)
        hud.label?.numberOfLines = 0
        hud.bezelView?.style = .solidColor
        hud.bezelView?.backgroundColor = ColorBlack
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: afterDelay)
        return hud
    }
    
    /// 显示带图片或不带图片的Toast
    @discardableResult
    class func showCustomToast(message: String?, in superView: UIView? = nil) -> MBProgressHUD? {
        let view = superView ?? KWindow!

        let hud = MBProgressHUD.show(for: view, animated: true)
        hud.mode = .text
        hud.minSize = CGSizeMake(KScreenWidth - 50, KScreenWValue(55))
        hud.label?.text = message
        hud.mode = .text
        hud.label?.textColor = ColorBlack
        hud.label?.font = FontHelveticaBoldScale(18)
        hud.label?.numberOfLines = 0
        hud.bezelView?.style = .solidColor
        
        
        hud.bezelView?.backgroundColor = ColorClear
        
        //        let vi = UIView.init()
        //
        //        hud.bezelView?.addSubview(vi)
        //        hud.bezelView?.sendSubviewToBack(vi)
        //        vi.snp.makeConstraints { make in
        //            make.edges.equalToSuperview()
        //        }
        
        
        //        hud.bezelView?.snp.updateConstraints{ make in
        //            make.size.equalTo(CGSizeMake(KScreenWidth - 50, KScreenWValue(55)))
        //        }
        //        hud.label?.snp.updateConstraints{ make in
        //            make.size.equalTo(CGSizeMake(KScreenWidth - 50, KScreenWValue(55)))
        //        }
        hud.bezelView?.layer.cornerRadius = KScreenWValue(22.5);
        hud.bezelView?.layer.masksToBounds = true
        hud.removeFromSuperViewOnHide = true
        hud.hide(animated: true, afterDelay: afterDelay)
        
        //        let selfHeight =  hud.bezelView!.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
        //        var frame =  hud.bezelView!.frame
        //        frame.size.height = selfHeight
        //        frame.size.width = KScreenWidth - 50
        //        vi.frame = frame
        //
        
        //        let gradientLayer = CALayer.configGradientLayer(size: vi.size, gradientColors: [Color4BE6CF,ColorD4F8AE], directionType: .topToBottom)
        //        hud.bezelView!.layer.addSublayer(gradientLayer)
        //        vi.layer.addSublayer(gradientLayer)
        
        
        return hud
    }
    /// 隐藏HUD（全局）
    class func hideHUD() {
        hideHUD(for: nil)
    }
    /// 隐藏指定视图上的HUD
    class func hideHUD(for superView: UIView?) {
        if let v = superView {
            MBProgressHUD.hide(for: v, animated: true)
        }
    }
}
